package demo

import (
	"regexp"
	"strings"

	"golang.org/x/text/cases"
	"golang.org/x/text/language"
)

type Service interface {
	Exec()
}

var registry = make(map[string]func() Service)

func Register(name string, factory func() Service) {
	registry[name] = factory
}

func GetDemoHandler(name string) (Service, bool) {
	if factory, exists := registry[toCamelCase(name)]; exists {
		return factory(), true
	}
	return nil, false
}

func toCamelCase(s string) string {
	re := regexp.MustCompile(`[_-]+`)
	parts := re.Split(s, -1)

	titleCaser := cases.Title(language.English)
	for i, part := range parts {
		if i > 0 && len(part) > 0 {
			parts[i] = titleCaser.String(part)
		} else if len(part) > 0 {
			parts[i] = titleCaser.String(part)
		}
	}
	return strings.Join(parts, "")
}
